package org.example;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MusicTest extends InitAndEndBrowser {
    @Test
    @Order(2)
    void Login() throws InterruptedException {
        webDriver.get("http://43.138.38.220:9090/login.html");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#user")).sendKeys("gb");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        webDriver.switchTo().alert().accept();
        sleep(3000);
        String url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.38.220:9090/list.html",url);
    }
    @Test
    @Order(1)
    void loginError() throws InterruptedException {
        webDriver.get("http://43.138.38.220:9090/login.html");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#user")).sendKeys("gb");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("1234567");
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        webDriver.switchTo().alert().accept();
        sleep(3000);
        String url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.38.220:9090/login.html",url);
    }
    @Test
    @Order(3)
    void uploadMusic() throws InterruptedException {
      webDriver.findElement(By.cssSelector("#body > div.container > div:nth-child(3) > a:nth-child(2)")).
              click();
      sleep(3000);
      webDriver.findElement(By.cssSelector("body > form > input[type=file]:nth-child(1)"));
      sleep(3000);
      webDriver.findElement(
              By.cssSelector("body > form > input[type=file]:nth-child(1)")).sendKeys
              ("D:\\music\\橘色温度.mp3");
      webDriver.findElement(By.cssSelector("body > form > label > input[type=text]")).
              sendKeys("666");
      webDriver.findElement(By.cssSelector("body > form > input[type=submit]:nth-child(3)")).click();
      sleep(4000);
      Assertions.assertEquals("http://43.138.38.220:9090/list.html",webDriver.getCurrentUrl());
      String musicname = webDriver.findElement(By.cssSelector("#info > tr:nth-child(4) > td:nth-child(2)")).
              getText();
      Assertions.assertEquals("橘色温度",musicname);
    }
    @Test
    @Order(4)
    void playMusic() throws InterruptedException {
        sleep(3000);
        webDriver.findElement(By.cssSelector("#info > tr:nth-child(1) > td:nth-child(4) > button")).
                click();
        sleep(8000);
        WebElement webElement = webDriver.findElement(By.
                cssSelector
                        ("#body > div:nth-child(2) > div > div > div.controlbar > ul > li:nth-child(2) > a"));
        Assertions.assertNotNull(webElement);
    }
    @Test
    @Order(5)
    void deleteMusic() throws InterruptedException {
        webDriver.findElement(By.cssSelector
                ("#info > tr:nth-child(4) > td:nth-child(5) > button:nth-child(1)")).click();
        sleep(3000);
        webDriver.switchTo().alert().accept();
        sleep(3000);
        Assertions.assertEquals("http://43.138.38.220:9090/list.html",webDriver.getCurrentUrl());
    }
    @Test
    @Order(6)
    void findMusic() throws InterruptedException {
        webDriver.findElement(By.cssSelector("#exampleInputName2")).sendKeys("His");
        webDriver.findElement(By.cssSelector("#submit1")).click();
        sleep(3000);
        String musicname = webDriver.findElement(By.cssSelector
                ("#info > tr:nth-child(1) > td:nth-child(2)")).getText();
        Assertions.assertEquals("His Theme",musicname);
    }
    @Test
    @Order(7)
    void loveMusic() throws InterruptedException {
        webDriver.get("http://43.138.38.220:9090/list.html");
        sleep(3000);
        webDriver.findElement(By.cssSelector
                ("#info > tr:nth-child(2) > td:nth-child(5) > button:nth-child(2)")).click();
        sleep(3000);
        webDriver.switchTo().alert().accept();
        webDriver.findElement(By.cssSelector
                ("#body > div.container > div:nth-child(3) > a:nth-child(1)")).click();
        sleep(3000);
        String musicname = webDriver.findElement(By.cssSelector
                ("#info > tr:nth-child(2) > td:nth-child(1)")).getText();
        Assertions.assertEquals("His Theme",musicname);
    }
}

